package com.client.koshercook.client;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/** this is an abstract class! using this for history and favorites */
public abstract class RecsAndLinksBox extends Composite {
	
	protected Image open;
	protected Button close = new Button("סגור");
	protected DialogBox dBox = new DialogBox();
	protected FlexTable table = new FlexTable();
	protected ScrollPanel scrollPanel;
	protected VerticalPanel Vpanel = new VerticalPanel();

	public RecsAndLinksBox(String header) {
		
		Vpanel.setStyleName("recsAndLinks");
		
		dBox.hide();
		open = new Image();
		dBox.setText(header);
		dBox.setModal(false);
		
		table.setText(0, 0, "קישור חיצוני");
		table.setText(0, 1, "שם המתכון");
		table.getRowFormatter().addStyleName(0, "IngListColumsHeader");
		table.setCellPadding(3);
		table.setBorderWidth(3);
		scrollPanel = new ScrollPanel(table);
		scrollPanel.setHeight("250px");
		Vpanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		Vpanel.add(scrollPanel);
		Vpanel.add(close);
		dBox.add(Vpanel);
		
		open.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(dBox.isShowing()){
					dBox.hide();		//if already showing, close it
					off();
					return;
				}
				if(!Client.model.isConnected()){
					//Window.alert("עליך להיות מחובר לאתר");
					if(dBox.getText().startsWith("ס"))
						Client.model.showLogMessage("לראות את ספר המתכונים שלך");
					else
						Client.model.showLogMessage("לראות את היסטורית המתכונים שלך");
					off();
					return;
				}else{
					dBox.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
						public void setPosition(int offsetWidth, int offsetHeight) {
							int left = 10;
							int top = (Window.getClientHeight() - offsetHeight) / 2;
							dBox.setPopupPosition(left, top);
						}
					});
					getData();
					off();
				}				
			}
		});
		
		close.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dBox.hide();
				Client.model.setInfoText("d");
			}
		});
		
		initWidget(open);
	}

	/** implement this with the RPC.
	 *  on success call fillTable(result) */
	protected abstract void getData();
	
	/** use this to fill the table */
	protected void fillTable(List<String> result){
		//clear table
		while(table.getRowCount()>1)
			table.removeRow(table.getRowCount()-1);
		//fill table
		int i = 1;
		for (String s : result) {
			int j = s.indexOf("@");
			String link = s.substring(j + 1);
			String recName = s.substring(0, j);
			table.setWidget(i, 1, new RecipeButton(link, recName));
			table.setWidget(i, 0, new ImageLink(link, recName));
			table.getCellFormatter().addStyleName(i, 0,"IngListColums");
			table.getCellFormatter().addStyleName(i, 1,"IngListColums");
			i++;
		}
	}
	
	/*remove glow from name*/
	protected abstract void off();
}